/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dal;
//gère la table lineFriends et  de la database

import dal.util.Database;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.Friends;
import models.LineFriends;
import models.Users;

/**
 * this class interacts with database
 * @author gabriellebotbol
 */
public class FriendsDAL 
{
    /**
     * returns the user's friend representation by username
     * @param username
     * @return 
     */
    public static Friends getFriendByUsername(String username)
    {
        Friends friend=null;
        
        try 
        {
            ResultSet rs = Database.executeQuery("select * from Friends where username='"+username+"';");
            if(rs.next())
            {
                friend=new Friends(rs.getInt("idFriends"),rs.getString("username"));
                
            }
            Database.closeConnection();
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
        return friend;
    }
    
    /**
     * returns a user's friend representation by id
     * @param id
     * @return 
     */
    public static Friends getFriendById(int id)
    {
        Friends friend=null;
        
        try 
        {
            ResultSet rs = Database.executeQuery("select * from Friends where idFriends='"+id+"';");
            if(rs.next())
            {
                friend=new Friends(rs.getInt("idFriends"),rs.getString("username"));
                
            }
            Database.closeConnection();
        } 
        catch (SQLException ex) 
        {
            
            Logger.global.log(Level.SEVERE, null, ex);
        }
        return friend;
    }
    
    /**
     * returns the list of Friends of an user by username
     * @param username
     * @return 
     */
    public static ArrayList<LineFriends> getAllFriendsByUser(Users user)
    {
        ArrayList<LineFriends> users = null;
        try 
        {
            ResultSet rs = Database.executeQuery("select * from LineFriends where idFriend='"+user.getIdFriend()+"';");
            users=new ArrayList<LineFriends>();
            while(rs.next())
            {
                users.add(new LineFriends(UsersDAL.getUserByUsername(rs.getString("username")),rs.getDate("addDate")));
                
            }
            Database.closeConnection();
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
        return users;
    }
    
    public static void insertFriend(Friends friends)
    {
        try 
        {
            Database.executeNonQuery("insert into Friends(username) values('"+ friends.getUsername()+"');");
        } catch (SQLException ex) {
            Logger.getLogger(FriendsDAL.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    //ajouter un nouvel ami
    public static void insertFriendInLineFriend(String username, Date addDate)
    {
        int idFriend;
        //on récupère d'abord l'idFriend
        try 
        {
            ResultSet rs = Database.executeQuery("select idFriend from Friends where username='"+username+"';");
            if(rs.next())
            {
                idFriend=rs.getInt("idFriend");   
            
                String request="insert into LineFriends values("+idFriend+",'"+username+"','"+addDate+"');";
                try 
                {
                    Database.executeNonQuery(request);
                } 
                catch (SQLException ex) 
                {
                    Logger.global.log(Level.SEVERE, null, ex);
                }   
                Database.closeConnection();
            }
        }
        catch (SQLException ex) 
        {
            System.out.println(ex.getMessage());
            Logger.global.log(Level.SEVERE, null, ex);
        }
        
        //on fait l'insertion dans LineFriends de l'id récupéré, la date du jour 
        //et le nom d'utilisateur
        /*String request="insert into LineFriends values("+idFriend+",'"+username+"','"+addDate+"');";
        try 
        {
            Database.executeNonQuery(request);
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }  */ 
    }
    
    public static void deleteFriends(Friends friend)
    {
        try 
        {
            Database.executeNonQuery("delete from LineFriends where idFriend='"+friend.getIdFriends()+"';");
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
    }
}
